package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.TeacherRequestVO;
import com.yugong.entity.vo.response.TeacherListResponseVO;
import com.yugong.mapper.*;
import com.yugong.service.TeacherService;
import com.yugong.util.DigestUtils;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 教师业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private CollageMapper collageMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private OrderClassAdminMapper orderClassAdminMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private TimeUtils timeUtils;
    @Resource
    private DigestUtils digestUtils;

    /**
     * 根据订单班管理员工号查询教师
     *
     * @param orderClassAdminNo 订单班管理员工号
     * @return 教师
     */
    @Override
    public List<TeacherListResponseVO> findTeacherByOrderClassAdminNo(String orderClassAdminNo) {
        OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(orderClassAdminNo);
        if (orderClassAdmin.getOrderClassAdminCollageNo() == null) {
            return null;
        }
        Collage collage = collageMapper.selectById(orderClassAdmin.getOrderClassAdminCollageNo());
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper
                .ne(Teacher::getTeacherNo, "testTeacher")
                .eq(Teacher::getTeacherCollageNo, collage.getCollageNo());
        List<Teacher> teacherList = teacherMapper.selectList(teacherLambdaQueryWrapper);
        List<TeacherListResponseVO> teacherListResponseVOList = new ArrayList<>();
        for (Teacher teacher : teacherList) {
            TeacherListResponseVO teacherListResponseVO = new TeacherListResponseVO();
            teacherListResponseVO.setTeacherNo(teacher.getTeacherNo());
            teacherListResponseVO.setTeacherName(teacher.getTeacherName());
            teacherListResponseVO.setTeacherAge(teacher.getTeacherAge());
            if (1 == teacher.getTeacherSex()) {
                teacherListResponseVO.setTeacherSex("男");
            } else {
                teacherListResponseVO.setTeacherSex("女");
            }
            teacherListResponseVO.setTeacherPositionalTitle(teacher.getTeacherPositionalTitle());
            teacherListResponseVO.setTeacherCollageNo(teacher.getTeacherCollageNo());
            teacherListResponseVO.setTeacherRoomNo(teacher.getTeacherRoomNo());
            teacherListResponseVO.setTeacherCreateMan(teacher.getTeacherCreateMan());
            String createTime = timeUtils.setDateToStrDate(teacher.getTeacherCreateTime(), Const.YYYY_MM_DD);
            teacherListResponseVO.setTeacherCreateTime(createTime);
            if (teacher.getTeacherUpdateMan() == null) {
                teacherListResponseVO.setTeacherUpdateMan("未更新");
            } else {
                teacherListResponseVO.setTeacherUpdateMan(teacher.getTeacherUpdateMan());
            }
            if (teacher.getTeacherUpdateTime() == null) {
                teacherListResponseVO.setTeacherUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(teacher.getTeacherUpdateTime(), Const.YYYY_MM_DD);
                teacherListResponseVO.setTeacherUpdateTime(updateTime);
            }
            teacherListResponseVOList.add(teacherListResponseVO);
        }
        return teacherListResponseVOList;
    }

    /**
     * 根据教师名查询教师信息
     *
     * @param pageNum     当前页
     * @param pageSize    当前页的数据总数
     * @param teacherName 教师名
     * @return 教师信息
     */
    @Override
    public PageInfo<TeacherListResponseVO> findTeacher(Integer pageNum, Integer pageSize, String teacherName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper
                .like(Teacher::getTeacherName, teacherName)
                .orderByDesc(Teacher::getTeacherCreateTime);
        List<Teacher> teacherList = teacherMapper.selectList(teacherLambdaQueryWrapper);
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.like("teacher_name", teacherName);
        Long teacherCountNumber = teacherMapper.selectCount(teacherQueryWrapper);
        List<TeacherListResponseVO> teacherListResponseVOList = new ArrayList<>();
        for (Teacher teacher : teacherList) {
            TeacherListResponseVO teacherListResponseVO = new TeacherListResponseVO();
            teacherListResponseVO.setTeacherNo(teacher.getTeacherNo());
            if ("".equals(teacher.getTeacherName())) {
                teacherListResponseVO.setTeacherName("未填写");
            } else {
                teacherListResponseVO.setTeacherName(teacher.getTeacherName());
            }
            teacherListResponseVO.setTeacherAge(teacher.getTeacherAge());
            if (1 == teacher.getTeacherSex()) {
                teacherListResponseVO.setTeacherSex("男");
            } else if (0 == teacher.getTeacherSex()) {
                teacherListResponseVO.setTeacherSex("女");
            } else {
                teacherListResponseVO.setTeacherSex("未填写");
            }
            if ("".equals(teacher.getTeacherPositionalTitle())) {
                teacherListResponseVO.setTeacherPositionalTitle("未填写");
            } else {
                teacherListResponseVO.setTeacherPositionalTitle(teacher.getTeacherPositionalTitle());
            }
            if (teacher.getTeacherCollageNo() != null) {
                Collage collage = collageMapper.selectById(teacher.getTeacherCollageNo());
                if (collage == null) {
                    teacherListResponseVO.setTeacherCollageNo("未填写");
                    teacherListResponseVO.setTeacherCollageName("未填写");
                } else {
                    teacherListResponseVO.setTeacherCollageNo(teacher.getTeacherCollageNo());
                    teacherListResponseVO.setTeacherCollageName(collage.getCollageName());
                }
            } else {
                teacherListResponseVO.setTeacherCollageNo("未填写");
                teacherListResponseVO.setTeacherCollageName("未填写");
            }
            if (teacher.getTeacherRoomNo() != null) {
                Room room = roomMapper.selectById(teacher.getTeacherRoomNo());
                if (room == null) {
                    teacherListResponseVO.setTeacherRoomNo("未填写");
                    teacherListResponseVO.setTeacherRoomName("未填写");
                } else {
                    teacherListResponseVO.setTeacherRoomNo(teacher.getTeacherRoomNo());
                    teacherListResponseVO.setTeacherRoomName(room.getRoomName());
                }
            } else {
                teacherListResponseVO.setTeacherRoomNo("未填写");
                teacherListResponseVO.setTeacherRoomName("未填写");
            }
            Account account = accountMapper.selectById(teacher.getTeacherNo());
            if (account == null) {
                teacherListResponseVO.setTeacherPhone("未填写");
                teacherListResponseVO.setTeacherEmail("未填写");
            } else {
                if ("".equals(account.getAccountPhone())) {
                    teacherListResponseVO.setTeacherPhone("未填写");
                } else {
                    teacherListResponseVO.setTeacherPhone(digestUtils.base64Decode(account.getAccountPhone()));
                }
                if ("".equals(account.getAccountEmail())) {
                    teacherListResponseVO.setTeacherEmail("未填写");
                } else {
                    teacherListResponseVO.setTeacherEmail(digestUtils.base64Decode(account.getAccountEmail()));
                }
            }
            teacherListResponseVO.setTeacherCreateMan(teacher.getTeacherCreateMan());
            String createTime = timeUtils.setDateToStrDate(teacher.getTeacherCreateTime(), Const.YYYY_MM_DD);
            teacherListResponseVO.setTeacherCreateTime(createTime);
            if (teacher.getTeacherUpdateMan() == null) {
                teacherListResponseVO.setTeacherUpdateMan("未更新");
            } else {
                teacherListResponseVO.setTeacherUpdateMan(teacher.getTeacherUpdateMan());
            }
            if (teacher.getTeacherUpdateTime() == null) {
                teacherListResponseVO.setTeacherUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(teacher.getTeacherUpdateTime(), Const.YYYY_MM_DD);
                teacherListResponseVO.setTeacherUpdateTime(updateTime);
            }
            teacherListResponseVO.setTeacherCountNumber(teacherCountNumber);
            teacherListResponseVOList.add(teacherListResponseVO);
        }
        return PageInfo.of(teacherListResponseVOList);
    }

    /**
     * 删除教师
     *
     * @param teacherNo 工号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTeacher(String teacherNo) {
        int deleteAccountFlag = accountMapper.deleteById(teacherNo);
        int deleteTeacherFlag = teacherMapper.deleteById(teacherNo);
        return deleteAccountFlag + deleteTeacherFlag;
    }

    /**
     * 查询教师个人信息
     *
     * @param teacherNo 工号
     * @return 教师个人信息
     */
    @Override
    public TeacherListResponseVO TeacherInfo(String teacherNo) {
        Teacher teacher = teacherMapper.selectById(teacherNo);
        TeacherListResponseVO teacherListResponseVO = new TeacherListResponseVO();
        if (teacher == null) {
            teacherListResponseVO.setTeacherName("");
            teacherListResponseVO.setTeacherAge(null);
            teacherListResponseVO.setTeacherSex("");
            teacherListResponseVO.setTeacherPhone("");
            teacherListResponseVO.setTeacherEmail("");
            teacherListResponseVO.setTeacherPositionalTitle("");
            teacherListResponseVO.setTeacherCollageNo("");
            teacherListResponseVO.setTeacherCollageName("");
            teacherListResponseVO.setTeacherRoomNo("");
            teacherListResponseVO.setTeacherRoomName("");
        } else {
            teacherListResponseVO.setTeacherNo(teacherNo);
            if (teacher.getTeacherName() == null) {
                teacherListResponseVO.setTeacherName("");
            } else {
                teacherListResponseVO.setTeacherName(teacher.getTeacherName());
            }
            if (0 == teacher.getTeacherAge()) {
                teacherListResponseVO.setTeacherAge(null);
            } else {
                teacherListResponseVO.setTeacherAge(teacher.getTeacherAge());
            }
            if (1 == teacher.getTeacherSex()) {
                teacherListResponseVO.setTeacherSex("男");
            } else if (0 == teacher.getTeacherSex()) {
                teacherListResponseVO.setTeacherSex("女");
            } else {
                teacherListResponseVO.setTeacherSex("");
            }
            Account account = accountMapper.selectById(teacherNo);
            if (account == null) {
                teacherListResponseVO.setTeacherPhone("");
                teacherListResponseVO.setTeacherEmail("");
            } else {
                if (account.getAccountPhone() == null) {
                    teacherListResponseVO.setTeacherPhone("");
                } else {
                    teacherListResponseVO.setTeacherPhone(digestUtils.base64Decode(account.getAccountPhone()));
                }
                if (account.getAccountEmail() == null) {
                    teacherListResponseVO.setTeacherEmail("");
                } else {
                    teacherListResponseVO.setTeacherEmail(digestUtils.base64Decode(account.getAccountEmail()));
                }
            }
            if (teacher.getTeacherPositionalTitle() == null) {
                teacherListResponseVO.setTeacherPositionalTitle("");
            } else {
                teacherListResponseVO.setTeacherPositionalTitle(teacher.getTeacherPositionalTitle());
            }
            Collage collage = collageMapper.selectById(teacher.getTeacherCollageNo());
            if (collage == null) {
                teacherListResponseVO.setTeacherCollageNo("");
                teacherListResponseVO.setTeacherCollageName("");
            } else {
                if (collage.getCollageNo() == null) {
                    teacherListResponseVO.setTeacherCollageNo("");
                } else {
                    teacherListResponseVO.setTeacherCollageNo(collage.getCollageNo());
                }
                if (collage.getCollageName() == null) {
                    teacherListResponseVO.setTeacherCollageName("");
                } else {
                    teacherListResponseVO.setTeacherCollageName(collage.getCollageName());
                }
            }
            Room room = roomMapper.selectById(teacher.getTeacherRoomNo());
            if (room == null) {
                teacherListResponseVO.setTeacherRoomNo("");
                teacherListResponseVO.setTeacherRoomName("");
            } else {
                if (room.getRoomNo() == null) {
                    teacherListResponseVO.setTeacherRoomNo("");
                } else {
                    teacherListResponseVO.setTeacherRoomNo(room.getRoomNo());
                }
                if (room.getRoomName() == null) {
                    teacherListResponseVO.setTeacherRoomName("");
                } else {
                    teacherListResponseVO.setTeacherRoomName(room.getRoomName());
                }
            }
        }
        return teacherListResponseVO;
    }

    /**
     * 修改信息
     *
     * @param teacherRequestVO 教师请求实体类
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateInfo(TeacherRequestVO teacherRequestVO) {
        Date updateTime = new Date();
        Account account = new Account();
        account.setAccountNo(teacherRequestVO.getTeacherNo());
        account.setAccountPhone(digestUtils.base64Encode(teacherRequestVO.getTeacherPhone()));
        account.setAccountEmail(digestUtils.base64Encode(teacherRequestVO.getTeacherEmail()));
        account.setAccountUpdateMan(teacherRequestVO.getTeacherNo());
        account.setAccountUpdateTime(updateTime);
        int updateAccountFlag = accountMapper.updateById(account);
        Teacher teacher = new Teacher();
        teacher.setTeacherNo(teacherRequestVO.getTeacherNo());
        teacher.setTeacherName(teacherRequestVO.getTeacherName());
        teacher.setTeacherAge(teacherRequestVO.getTeacherAge());
        teacher.setTeacherSex(teacherRequestVO.getTeacherSex());
        teacher.setTeacherUpdateMan(teacherRequestVO.getTeacherNo());
        teacher.setTeacherUpdateTime(updateTime);
        int updateTeacherFlag = teacherMapper.updateById(teacher);
        return updateAccountFlag + updateTeacherFlag;
    }

    /**
     * 修改教师
     *
     * @param teacherRequestVO 教师请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateTeacher(TeacherRequestVO teacherRequestVO) {
        Teacher teacher = new Teacher();
        teacher.setTeacherNo(teacherRequestVO.getTeacherNo());
        teacher.setTeacherPositionalTitle(teacherRequestVO.getTeacherPositionalTitle());
        teacher.setTeacherCollageNo(teacherRequestVO.getTeacherCollageNo());
        teacher.setTeacherRoomNo(teacherRequestVO.getTeacherRoomNo());
        teacher.setTeacherUpdateMan(teacherRequestVO.getTeacherUpdateMan());
        teacher.setTeacherUpdateTime(new Date());
        return teacherMapper.updateById(teacher);
    }
}
